package cn.taoticket.service;

import cn.taoticket.common.EmptyUtils;
import cn.taoticket.common.MD5;
import cn.taoticket.pojo.TaoticketUser;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@RestController
public class RestTaoticketTokenService {
    private String tokenPrefix = "token";

    /**
     * 生成用户token的方法，尽量复杂避免重复
     */
    @RequestMapping(value = "/generateToken",method = RequestMethod.POST)
    public String generateToken(@RequestBody TaoticketUser taoticketUser){
        StringBuilder sb = new StringBuilder();
        sb.append(tokenPrefix+"-");
        sb.append("PC-"+"-");
        //转MD5，这里存在有两种情况，一是用户正常用自己账号登录，二是可能用户用了微信账号登录，所以要做判断
        String info = MD5.getMd5(EmptyUtils.isEmpty(taoticketUser.getPhone())?taoticketUser.getWxUserId():taoticketUser.getPhone(),32);
        sb.append(info+"-");
        sb.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"-");
        sb.append(UUID.randomUUID().toString().substring(0,6));
        return sb.toString();
    }
}
